package main

import (
	//"image/color"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	//"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"os"
	//"os/exec"
	"fmt"
	//"bytes"
	"github.com/flopp/go-findfont"
	"strings"
	"reflect"
	"gopkg.in/ini.v1"
	"runtime"
	"gogui/mgogui" 	//自定义包
	"gogui/mtheme"	//自定义包中文字体包
)


//go mod init 包名
//go mod tidy


//go env -w GOPATH=F:\go-server\gopath
//go env -w GOMODCACHE=F:\go-server\gopath\pkg\mod
//go env -w GOPROXY=https://goproxy.cn


//go build  -ldflags="-s -w -H=windowsgui" main_gui.go
//go get fyne.io/fyne/cmd/fyne   cmd命令
//SET ANDROID_HOME=F:\androidsdk
//SET ANDROID_NDK_HOME=F:\androidsdk\ndk\24.0.8215888
//fyne package -os android -appID com.example.myapp -icon b1.ico



func init() {
    fontPaths := findfont.List()
    for _, path := range fontPaths {
        //fmt.Println(path)
        //楷体:simkai.ttf
        //黑体:simhei.ttf
        if strings.Contains(path, "simkai.ttf") {
            fmt.Println(path)
            os.Setenv("FYNE_FONT", path) // 设置环境变量  // 取消环境变量 os.Unsetenv("FYNE_FONT")
            break
        }
    }
    fmt.Println("=============")
}

func open_ip(st_ip string) string {
	mmp := "http://192.168.50.99:8080/"
	cfg, err := ini.Load("my.ini")
    if err != nil {
    	var conn ini.LoadOptions
    	cfg := ini.Empty(conn)
    	cfg.NewSection("data")
    	cfg.Section("data").Key("ip").SetValue(mmp)
    	cfg.SaveTo("my.ini")
    	return mmp
    }
    if st_ip != "" {
    	cfg.Section("data").Key("ip").SetValue(st_ip)
    	cfg.SaveTo("my.ini")
    	return st_ip
    }else{
    	key := cfg.Section("data").Key("ip")
    	fmt.Println(key.String())
    	return key.String()
    }
}

func main() {
	mygui.MconfigIP = open_ip("")
	fmt.Println("ip",mygui.MconfigIP)
	var t_data = make(chan int)
	//var s_data = make(chan int)
	myApp := app.New()
	myApp.Settings().SetTheme(&theme.MyTheme{})
	//myApp.Settings().SetTheme(theme.Mtheme)
	myWindow := myApp.NewWindow("主界面")
	gui := m_gui_update(t_data)
	go m_thread(myApp, myWindow, t_data, gui)
	obj_dir(myApp.Settings())
	myWindow.SetContent(gui)
	myWindow.Resize(fyne.NewSize(300, 100))
	myWindow.CenterOnScreen()
	myWindow.ShowAndRun()
}

func m_gui_update(t_data chan int) *fyne.Container {
  	
	input := widget.NewSelectEntry([]string{mygui.MconfigIP})
	//lalist1 := widget.NewSelect([]string{" "}, func(value string) {
	//	fmt.Println("Select set to", value)
	//})
	input.SetText(mygui.MconfigIP)
	//obj_dir(input)
	//lalist1.SetSelectedIndex(0)
	//lalist1.Disable()
	button1 := widget.NewButton("查看", func() {
		mygui.MconfigIP = input.Text
		open_ip(mygui.MconfigIP)
		t_data <- 1
		fmt.Println(mygui.MconfigIP)
	})

	button2 := widget.NewButton("修改", func() {
		mygui.MconfigIP = input.Text
		open_ip(mygui.MconfigIP)
		t_data <- 2
		fmt.Println(mygui.MconfigIP)
	})

	button3 := widget.NewButton("上传", func() {
		mygui.MconfigIP = input.Text
		open_ip(mygui.MconfigIP)
		t_data <- 3
		fmt.Println(mygui.MconfigIP)
	})

	button4 := widget.NewButton("白名单", func() {
		mygui.MconfigIP = input.Text
		open_ip(mygui.MconfigIP)
		t_data <- 4
		fmt.Println(mygui.MconfigIP)
	})
	//obj_dir(button1)
	//box := container.New(layout.NewHBoxLayout(), lalist1 )
	mbox := container.New(layout.NewVBoxLayout(), input, layout.NewSpacer(), button1, layout.NewSpacer(), button2, layout.NewSpacer(),button3, layout.NewSpacer(), button4,layout.NewSpacer())
	//go button_update(s_data,button1,button2,button3,button4)
	return mbox
}



func m_thread(myappWindow fyne.App, myWindow fyne.Window, t_data chan int, mbox *fyne.Container) {
	//mbox := m_gui_update(t_data)
	mSize := the_zoom(myWindow)
	app1 := mygui.See_main(myappWindow, t_data)
	app2 := mygui.Modify_main(myappWindow, t_data)
	app3 := mygui.Upload_main(myappWindow, t_data)
	app4 := mygui.White_main(myappWindow, t_data)

	myWindow.SetCloseIntercept(func(){
		myWindow.Close()
		app1.Close()
		app2.Close()
		app3.Close()
		app4.Close()
		fmt.Println("关闭所有窗口")
	})

	for {
		us := <- t_data
		switch us{
			case 1:
				app1.CenterOnScreen()
				app1.Resize(mSize)
				app1.Show()
			case 2:
				app2.CenterOnScreen()
				app2.Resize(mSize)
				app2.Show()
			case 3:
				app3.CenterOnScreen()
				app3.Resize(mSize)
				app3.Show()
			case 4:
				app4.CenterOnScreen()
				app4.Resize(mSize)
				app4.Show()
			case 5:
				app1.Hide()
			case 6:
				app2.Hide()
			case 7:
				app3.Hide()
			case 8:
				app4.Hide()
			default:
				fmt.Println(us)
		}
	}
	
}

func the_zoom(myWindow fyne.Window) fyne.Size {
	if runtime.GOOS == "windows" {
		return fyne.NewSize(500, 360)
	}else{
		return fyne.NewSize(1080, 2408)
	}
}

func obj_dir(obj interface{}){
    _dir := reflect.TypeOf(obj)
    for i := 0; i < _dir.NumMethod(); i++{
        fmt.Println(_dir.Method(i).Name)
    }
}